package com.neusoft.elmboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.elmboot.po.DeliveryAddress;
import com.neusoft.elmboot.service.DeliveryAddressService;
import com.neusoft.elmboot.service.UserService;
import com.neusoft.elmboot.util.AESUtils;
import com.neusoft.elmboot.util.RSAUtils;

@RestController
@RequestMapping("/delivery-addresses")
public class DeliveryAddressController {

	@Autowired
	private DeliveryAddressService deliveryAddressService;
	
	@Autowired
	private UserService userService;
	
	RSAUtils rsaUtils=new  RSAUtils();
	String key  = "woniucsdnvip8888"; 
	
	@GetMapping("/users/")
	public List<DeliveryAddress> listDeliveryAddressByUserId(@RequestParam(required = false) String userId,
			@RequestParam(required = false) String publicKey) {
		List<DeliveryAddress> list = deliveryAddressService.listDeliveryAddressByUserId(userId);
		for (DeliveryAddress temp : list) {
			temp.setAddress(AESUtils.decrypt(temp.getAddress(), key));//AES解密
			temp.setAddress(rsaUtils.rsa_endjiami(publicKey,temp.getAddress()));//RSA加密
			temp.setContactTel(AESUtils.decrypt(temp.getContactTel(), key));//AES解密
			temp.setContactTel(rsaUtils.rsa_endjiami(publicKey,temp.getContactTel()));//RSA加密
		}
		return list;
	} 
	
	@GetMapping("/")
	public DeliveryAddress getDeliveryAddressById(@RequestParam(required = false) Integer daId,
			@RequestParam(required = false) String publicKey) {
		DeliveryAddress temp=deliveryAddressService.getDeliveryAddressById(daId);
		temp.setAddress(AESUtils.decrypt(temp.getAddress(), key));//AES解密
		temp.setAddress(rsaUtils.rsa_endjiami(publicKey,temp.getAddress()));//RSA加密
		temp.setContactTel(AESUtils.decrypt(temp.getContactTel(), key));//AES解密
		temp.setContactTel(rsaUtils.rsa_endjiami(publicKey,temp.getContactTel()));//RSA加密
		return temp;
	} 
	
	@PostMapping("")
	public int saveDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception{
		deliveryAddress.setAddress(rsaUtils.rsa_endjiemi(userService.getPrivateKey(deliveryAddress.getUserId()),deliveryAddress.getAddress()));//RSA解密
		deliveryAddress.setAddress(AESUtils.encrypt(deliveryAddress.getAddress(), key));//AES加密
		deliveryAddress.setContactTel(rsaUtils.rsa_endjiemi(userService.getPrivateKey(deliveryAddress.getUserId()),deliveryAddress.getContactTel()));//RSA解密
		deliveryAddress.setContactTel(AESUtils.encrypt(deliveryAddress.getContactTel(), key));//AES加密
		return deliveryAddressService.saveDeliveryAddress(deliveryAddress);
	} 
	
	
	@PutMapping("/{daId}")
	public int updateDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception{
		deliveryAddress.setAddress(rsaUtils.rsa_endjiemi(userService.getPrivateKey(deliveryAddress.getUserId()),deliveryAddress.getAddress()));//RSA解密
		deliveryAddress.setAddress(AESUtils.encrypt(deliveryAddress.getAddress(), key));//AES加密
		deliveryAddress.setContactTel(rsaUtils.rsa_endjiemi(userService.getPrivateKey(deliveryAddress.getUserId()),deliveryAddress.getContactTel()));//RSA解密
		deliveryAddress.setContactTel(AESUtils.encrypt(deliveryAddress.getContactTel(), key));//AES加密
		return deliveryAddressService.updateDeliveryAddress(deliveryAddress);
	} 
	
	@DeleteMapping("/{daId}")
	public int removeDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception{
		return deliveryAddressService.removeDeliveryAddress(deliveryAddress.getDaId());
	} 
}
